package scau.erps.service.impl;

import java.util.List;

import javax.swing.JOptionPane;

import org.nutz.dao.Cnd;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.service.IdNameEntityService;

import scau.erps.app.Erps;
import scau.erps.entity.Balancesheet;
import scau.erps.entity.Loan;
import scau.erps.entity.Sand;
import scau.erps.entity.Team;
import scau.erps.service.BalancesheetService;
import scau.erps.service.LoanService;
import scau.erps.service.SandService;
import scau.erps.util.CalculateAmount;
import scau.erps.util.CalculateQuarter;

/**
 * 贷款服务实现类
 * @author DCJ
 *
 */
public class LoanServiceImpl extends IdNameEntityService<Loan> implements LoanService {
	
	/**
	 * 申请短贷
	 * @param loan
	 */
	@Override
	public void applyShortLoan(int amount) {
		if(ifAvailable(amount)){
		String time = Erps.getSand().getTime();
		String loanNo = "SL-" + time + "-" + Erps.getCurrentTeam().getId();
		Loan loan = new Loan();
		loan.setTeam(Erps.getCurrentTeam().getId());
		loan.setLoanNo(loanNo);
		loan.setBeginTime(time);
		loan.setEndTime(CalculateQuarter.addFourQuarter(time));
		loan.setAmount(amount);
		loan.setLoanType("短贷");
		loan.setInterest(CalculateAmount.getInterestOfShortTermLoan(amount));
		loan.setStatus("已申请");
		this.dao().insert(loan);		
		}		
	}
	@Override
	public void applyLongLoan(int amount, int year) {
		if(ifAvailable(amount)){
		String time = Erps.getSand().getTime();
		String applyYear = CalculateQuarter.getCurrentYear(time);
		String loanNo = "LL-" + applyYear + "-" + Erps.getCurrentTeam().getId();
		Loan loan = new Loan();
		loan.setTeam(Erps.getCurrentTeam().getId());
		loan.setLoanNo(loanNo);
		loan.setBeginTime(time);
		loan.setEndTime(CalculateQuarter.addYears(time, year));
		loan.setAmount(amount);
		loan.setLoanType("长贷");
		loan.setInterest(CalculateAmount.getInterestOfLongTermLoanPerYear(amount));
		loan.setStatus("已申请");
		this.dao().insert(loan);			
		
		}
	}

	/**
	 * 更新短贷
	 * @return
	 */
	@Override
	public void updateLoan(Loan loan) {
		this.dao().update(loan);
		
	}
	
	/**
	 * 查询所有贷款
	 * @return
	 */
	@Override
	public List<Loan> getAllLoans() {
		return this.query(null, null);
	}
	
	/**
	 * 按条件查询贷款
	 * @return
	 */
	@Override
	public List<Loan> getLoans(int team_Id, String loanType, String status) {
		SqlExpressionGroup e1 = null;
		SqlExpressionGroup e2 = null;
		SqlExpressionGroup e3 = null;
		if(team_Id != 0){
			e1 = Cnd.exps("team_Id", "=", team_Id);			
		}
		if(loanType != null){
			e2 = Cnd.exps("loanType", "=", loanType);
		}
		if(status != null){
			e3 = Cnd.exps("status", "=", status);
		}
		return this.query(Cnd.where(e1).and(e2).and(e3), null);
	}
	
	/**
	 * 偿还短贷款与高利贷
	 * @return
	 */
	@Override
	public void repayShortLoan(List<Loan> list) {
		Sand sand = Erps.getSand();
		int cash = sand.getCash();
		int sandInterest = sand.getInterest();
		int amount = 0;
		int loanInterest = 0;
		for (Loan loan : list) {
			amount = amount + loan.getAmount();	
			loanInterest = loanInterest + loan.getInterest();
		}
		if(cash>=(amount+loanInterest)){
			sand.setCash(cash-amount-loanInterest);
			sand.setInterest(sandInterest+loanInterest);
			this.dao().update(sand);
			for (Loan loan : list) {
				loan.setStatus("已还款");
				this.dao().update(loan);
			}
		}		
	}
	
	/**
	 * 偿还长贷款或付息
	 * @return
	 */
	@Override
	public boolean repayLongLoan(int team_Id) {
		boolean flag = false;
		Sand sand = Erps.getSand();
		int cash = sand.getCash();
		int sandInterest = sand.getInterest();
		int amount = 0;
		int loanInterest = 0;
		List<Loan> list = this.getLoans(team_Id, "长贷", "已审核");
		for (Loan loan : list) {
			loanInterest = loanInterest + loan.getInterest();
		}
		List<Loan> list2 = this.getReturnLoans(team_Id, "长贷");
		if(list2.size()>0){
			for (Loan loan : list2) {
				amount = amount + loan.getAmount();
			}
		}
		if(cash>=(amount+loanInterest)){
			sand.setCash(cash-amount-loanInterest);
			sand.setInterest(sandInterest+loanInterest);
			this.dao().update(sand);
			if(list2.size()>0){
				for (Loan loan : list2) {
					loan.setStatus("已还款");
					this.dao().update(loan);
				}				
			}
			flag = true;			
		}
		return flag;
	}
	
	
	/**
	 * 得到当前贷款数量
	 * @param team_Id
	 * @param loanType
	 * @return
	 */
	@Override
	public int getLoanAmount(int team_Id, String loanType){
		List<Loan> loans = getLoans(team_Id, loanType, "已审核");
		int total = 0;
		for (Loan loan : loans) {
			total = total + loan.getAmount();			
		}
		return total;		
	}

	/**
	 * 查询应还贷款
	 * @param team_Id
	 * @param loanType
	 * @return
	 */
	@Override
	public List<Loan> getReturnLoans(int team_Id, String loanType) {
		Sand sand = this.dao().fetch(Sand.class, team_Id);
		SqlExpressionGroup e1 = null;
		SqlExpressionGroup e2 = null;
		SqlExpressionGroup e3 = Cnd.exps("status", "=", "已审核");
		SqlExpressionGroup e4 = null;
		if(team_Id != 0){
			e1 = Cnd.exps("team_Id", "=", team_Id);			
		}
		if(loanType != null){
			e2 = Cnd.exps("loanType", "=", loanType);
		}
		e4 = Cnd.exps("endTime","=",sand.getTime());		
		return this.query(Cnd.where(e1).and(e2).and(e3).and(e4), null);
	}

	/**
	 * 根据贷款ID查贷款
	 * @param team_Id
	 * @param loanType
	 * @return
	 */
	@Override
	public Loan getLoanById(int loanId) {		
		return this.fetch(loanId);
	}
	/**
	 * 申请高利贷
	 * @author outsider
	 * @return
	 */
	@Override
	public void appLyUsuriousLoan(int amount, String quarter) {
		String time = Erps.getSand().getTime();
		String loanNo = "UL-" + time + "-" + Erps.getCurrentTeam().getId();
		Loan loan = new Loan();
		loan.setTeam(Erps.getCurrentTeam().getId());
		loan.setLoanNo(loanNo);
		loan.setBeginTime(time);
		loan.setEndTime(CalculateQuarter.calculateQuarter(time, quarter));
		loan.setAmount(amount);
		loan.setLoanType("高利贷");
		loan.setInterest(amount/5);
		loan.setStatus("已申请");
		this.dao().insert(loan);			
		
	}
	/**
	 * 判断是否满足贷款总额小于等于所有者权益的2倍(所有者权益是去年的报表数据)
	 * @author 郑剑川
	 * @return
	 */
	public boolean ifAvailable(int amount){
		Team team = Erps.getCurrentTeam();
		 int teamId=team.getId();
		SandService sandService = Erps.getIocInstance().get(SandServiceImpl.class);
		 Sand sand = sandService.getSandByTeam(teamId);
		BalancesheetService balancesheetService = Erps.getIocInstance().get(BalancesheetServiceImpl.class);
		List<Balancesheet> oldBalancesheetList = balancesheetService.getBalancesheetOfLastYear(teamId,sand.getTime());
		if((getTotal(team.getId())+amount)<=2*(oldBalancesheetList.get(0).getTotalEquity()))
			return true;
		else return false;
	}
	
	/**
	 *返回可贷款数量
	 * @author 郑剑川
	 * @return
	 */
	public int getAvailiable(){
		Team team = Erps.getCurrentTeam();
		 int teamId=team.getId();
		SandService sandService = Erps.getIocInstance().get(SandServiceImpl.class);
		Sand sand = sandService.getSandByTeam(teamId);
		BalancesheetService balancesheetService = Erps.getIocInstance().get(BalancesheetServiceImpl.class);
		List<Balancesheet> oldBalancesheetList = balancesheetService.getBalancesheetOfLastYear(teamId,sand.getTime());
		int amount=2*(oldBalancesheetList.get(0).getTotalEquity())-getTotal(team.getId());
		if((amount%10)>=0&&amount!=0)
		return (amount/10)*10;
		else 
			return 0;
	}

	/**
	 * 获得目前贷款总额返回的值为10的倍数
	 * @author 郑剑川
	 * @return
	 */
	public int getTotal(int team_Id){
		List<Loan> shenHeLoans = getLoans(team_Id, null, "已审核");
		int total = 0;
		for (Loan loan1 : shenHeLoans) {
			total = total + loan1.getAmount();			
		}
		List<Loan> shenQingLoans = getLoans(team_Id, null, "已申请");
		for (Loan loan2 : shenQingLoans) {
			total = total + loan2.getAmount();			
		}
		return total;		
	}
	/**
	 * 获得应还短贷笔数
	 */
	@Override
	public int getSizeOfShortLoanByTeam(int team_Id) {		
		return getReturnLoans(team_Id, "短贷").size()+getReturnLoans(team_Id, "高利贷").size();
	}
	/**
	 * 获得应还短贷数量
	 */
	@Override
	public int getAmountOfShortLoanByTeam(int team_Id) {
		List<Loan> shortLoans = getReturnLoans(team_Id, "短贷");
		List<Loan> highLoans = getReturnLoans(team_Id,"高利贷");
		int amount = 0;
		for (Loan loan : shortLoans) {
			amount = amount + loan.getAmount();			
		}
		for (Loan loan : highLoans) {
			amount = amount + loan.getAmount();			
		}
		return amount;
	}
	/**
	 * 获得应还短贷利息
	 */
	@Override
	public int getInterestOfShortLoanByTeam(int team_Id) {
		List<Loan> shortLoans = getReturnLoans(team_Id, "短贷");
		List<Loan> highLoans = getReturnLoans(team_Id,"高利贷");
		int interest = 0;
		for (Loan loan : shortLoans) {			
			interest = interest + loan.getInterest();
		}
		for (Loan loan : highLoans) {			
			interest = interest + loan.getInterest();
		}
		return interest;
	}
}


